package com.reggie.filter;

import com.alibaba.fastjson.JSON;
import com.reggie.common.enums.front.UserEnum;
import com.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.reggie.common.Constant.LOGIN_SESSION;
import static com.reggie.common.Constant.USER_SESSION;

/**
 * @Author 早睡蛋
 * @Date 2022/10/2 15:37:26
 * @Desc:
 */
@Slf4j
public class FrontFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request1 = (HttpServletRequest) request;
        HttpServletResponse response1 = (HttpServletResponse) response;
        log.info("拦截到请求：{}", request1.getRequestURI());
        String token = request1.getHeader("token");
        if (token != null) {
            log.info("用户已经登录，id:{}", request1.getSession().getAttribute(USER_SESSION));
            chain.doFilter(request1, response1);
            return;
        }
        log.info("未登录");
        response1.getWriter().write(JSON.toJSONString(UserEnum.NO_LOGIN.getResultResponseVo().setMessage("未登录")));
        return;

    }


}
